package library.ShareObject
{
	import flash.events.NetStatusEvent;
	import flash.net.SharedObject;
	import flash.net.SharedObjectFlushStatus;
	import flash.utils.ByteArray;

	public class GameShareObject
	{
		private static const SAVE_STATE_ENTERGAME:int = 0;	//进游戏,还未请求玩家开始保存功能
		private static const SAVE_STATE_SAVE:int = 1;			//玩家已允许开启保存功能
		private static const SAVE_STATE_NOSAVE:int = 2;		//玩家不允许开启保存功能

		private static var versionDict:Object = {};	//用来保存每一个文件的当前版本..#暂未用到
		private static var so_saveState:int = SAVE_STATE_ENTERGAME; 		//ShareObject的标记状态
		private static var pattern:RegExp = /[\/.\\]/g;
		private static var _cashSoList:Array = [];	//玩家还没请求保存数据时,对一些需要保存的数据,做缓存处理
		
		
		/**
		 * 返回一个存储对象
		 * @param path:String 路径
		 * @param version:String 版本号...临时参数,以后会用配置代替
		 */
		public static function getShareObjectData(path:String,version:String):ByteArray
		{
			if(so_saveState != SAVE_STATE_SAVE)
			{
				return null;
			}
			var soPath:String = path.replace(pattern, "-");
			
			var so:SharedObject = SharedObject.getLocal(soPath);
			
			var soData:Object = so.data["res"];
			
			if(soData) //数据已存
			{
				var soVersion:String = so.data["version"].toString();
				if(soVersion != version) //版本不同
				{
					soData = null;
				}
			}
			else
			{
				soData = null;	
			}
			
			return (soData as ByteArray);
		}
		
		/**
		 * 保存一个存储对象
		 * @param path:String 路径
		 * @param soData:ByteArray 对象数据
		 * @param version:String 版本号...临时参数,以后会用配置代替
		 */
		
		public static function saveShareObjectData(path:String,soData:ByteArray,version:String):void
		{
			if(so_saveState == SAVE_STATE_NOSAVE) //玩家不保存数据
			{
				return;
			}
			else if (so_saveState == SAVE_STATE_ENTERGAME) //玩家还没有被请求过保存数据
			{
				_cashSoList.push([path,soData,version]);
				return;	
			}
			
			
			var soPath:String = path.replace(pattern, "-");
			var so:SharedObject = SharedObject.getLocal(soPath);
			so.data.res = soData;
			so.data.version = version;
			
		}
		
		/**
		 * 检查并请求用户开启显示对象保存功能
		 * @param onCompleteFunc:Function 功能完成后回调函数
		 */
		public static function checkSharedObjectSaveState(onCompleteFunc:Function):void
		{
			var so:SharedObject = SharedObject.getLocal("WebSGSaveState");
			var soData:Object = so.data["res"];
			if(soData)
			{
				so_saveState = int(soData.toString());
				if(so_saveState != SAVE_STATE_ENTERGAME)
				{
					onCompleteFunc();
					return;
				}
			}
			
			so_saveState = SAVE_STATE_NOSAVE;
			try
			{
				so.data.res = SAVE_STATE_SAVE.toString();
				var flushResult:String = so.flush(1073741824);
				if (flushResult == SharedObjectFlushStatus.PENDING)
				{
					so.addEventListener(NetStatusEvent.NET_STATUS, saveSharedObjectResult);
					return;
				}
				else if (flushResult == SharedObjectFlushStatus.FLUSHED)
				{
					so_saveState = SAVE_STATE_SAVE;
				}
			}
			catch (e:Error)
			{
				so_saveState = SAVE_STATE_NOSAVE;
				
			}
			saveShareObjectCashList();
			onCompleteFunc();
			
			function saveSharedObjectResult(evt:NetStatusEvent):void
			{
				if(evt.info.code == "SharedObject.Flush.Success")
				{
					so_saveState = SAVE_STATE_SAVE;
				}
				else
				{
					so_saveState = SAVE_STATE_NOSAVE;
				}
				so.removeEventListener(NetStatusEvent.NET_STATUS, saveSharedObjectResult);
				saveShareObjectCashList();
				onCompleteFunc();
			}
		}
		
		
		/**
		 * 前置检查保存状态,不做任何处理
		 * @param onCompleteFunc:Function 功能完成后回调函数
		 */
		public static function preCheckShareObjectSaveState(onCompleteFunc:Function):void
		{
			var so:SharedObject = SharedObject.getLocal("WebSGSaveState");
			var soData:Object = so.data["res"];
			if(soData)
			{
				so_saveState = int(soData.toString());
			}
			onCompleteFunc();
		}
		
		
		private static function saveShareObjectCashList():void
		{
			if(so_saveState == SAVE_STATE_NOSAVE)
			{
				_cashSoList = [];
				return;
			}
			else if(so_saveState == SAVE_STATE_SAVE)
			{
				var ar:Array = null;
				for(var i:int = 0;i<_cashSoList.length;i++)
				{
					ar = _cashSoList[i];
					saveShareObjectData(ar[0],ar[1],ar[2]);
				}
			}
		}
		
	}
}